﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using IQKS.AWP.BusinessAccessLayer;
using IQKS.AWP.BusinessObject;
using System.Configuration;

public partial class Admin_PaymentInvoice : System.Web.UI.Page
{
   
    Guid gudUserID = CommonUtility.GetLoggedOnUserId();
    protected void Page_Load(object sender, EventArgs e)
    {


        if (!IsPostBack)
        {
            btnSave.Visible = false;
            GetRoalType();
            //Session["UserID"] = Guid.NewGuid();
        }
        txtInvoiceSDate.Attributes.Add("readonly", "readonly");
        txtAmount.Attributes.Add("readonly", "readonly");
        VisiblePD(false);
        //GetData();

    }
    public void VisiblePD(bool val)
    {
        PD1.Visible = val;
        PD2.Visible = val;
        PD3.Visible = val;
        PD4.Visible = val;
        PD5.Visible = val;
        PD6.Visible = val;
        PD7.Visible = val;
    }

    private void GetRoalType()
    {
        ddlRoalType.DataSource = GetRoleAssessorDataSet();
        ddlRoalType.DataTextField = "RoleTitle";
        ddlRoalType.DataValueField = "RoleId";
        ddlRoalType.DataBind();
        ddlRoalType.Items.Insert(0, new ListItem("-- Select Role type --", "0"));
    }

    private DataSet GetRoleAssessorDataSet()
    {

        DataSet objDs = null;
        try
        {
            objDs = new RoleMasterBAL().GetRoleAssessor();
        }
        catch (Exception exception)
        {
            exception.AddErrorToLog(); //throw new ApplicationException(exception.Message);
        }
        return objDs;

    }

    private DataSet GetDataSet(String InvoiceType)
    {

        DataSet objDs = null;
        try
        {
            objDs = new PaymentInvoiceBAL().GetPaymentInvoiceByAssessor(new Guid(ddlUserName.SelectedValue), InvoiceType);
        }
        catch (Exception exception)
        {
            exception.AddErrorToLog(); //throw new ApplicationException(exception.Message);
        }
        return objDs;

    }
    public string GetInvoiceReceipt(object InvoicePath)
    {
        string strPath = string.Empty;

        strPath =  ConfigurationSettings.AppSettings["AssessorInvoiceDoc"] + InvoicePath;

        return strPath;
    }
    private void GetData()
    {
        gvPaymentInvoice.DataSource = GetDataSet("PENDING");
        gvPaymentInvoice.DataBind();
        int count = gvPaymentInvoice.Rows.Count;
        if (count > 0)
        {
            btnSave.Visible = true;
            VisiblePD(true);
        }
        else
        {
            btnSave.Visible = false;
            VisiblePD(false);

        }
        gvPayedInvoice.DataSource = GetDataSet("PAYED");
        gvPayedInvoice.DataBind();


    }
    protected void ddlRoalType_SelectedIndexChanged(object sender, EventArgs e)
    {
        DataSet objDS = new DataSet();
        objDS = new UserMasterBAL().GetUserNameByRole(new Guid(ddlRoalType.SelectedValue)); ;
        CommonUtility.BindDropDown(ref ddlUserName, objDS, "FirstName", "UserId");
        CommonUtility.AddSelectToDropDownAsEmptyValue(ref ddlUserName);

        gvPaymentInvoice.DataSource = null;
        gvPaymentInvoice.DataBind();
        lblMessage.Text = "";
    }

    private void ClearData()
    {
        txtInvoiceSDate.Text = string.Empty;
        txtAmount.Text = string.Empty;
        txtComment.Text = string.Empty;
        lblMessage.Text = string.Empty;
        gvPaymentInvoice.DataSource = null;
        gvPaymentInvoice.DataBind();
        ddlRoalType.SelectedValue = "0";
        ddlUserName.SelectedValue = "0";
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {

        PaymentInvoiceBAL objPIBal;
        PaymentInvoiceBO objPIBO;
        //Guid gudUserID = new Guid(Convert.ToString(Session["UserID"]));
        string strReturnValue;


        objPIBal = new PaymentInvoiceBAL();
        objPIBO = new PaymentInvoiceBO();


        objPIBO.SetMethod = "INSERT";
        objPIBO.AssessorPaymentID = 0;
        objPIBO.PaymentAmount = Convert.ToDouble(txtAmount.Text.Trim());

        string[] date = new string[3];
        date[0] = txtInvoiceSDate.Text.Trim().Substring(0, 2);
        date[1] = txtInvoiceSDate.Text.Trim().Substring(3, 2);
        date[2] = txtInvoiceSDate.Text.Trim().Substring(6, 4);

        objPIBO.PaymentDate = Convert.ToDateTime(date[2] + "/" + date[1] + "/" + date[0]);
        objPIBO.PaymentComment = txtComment.Text.Trim();

        objPIBO.CreatedBy = CommonUtility.GetLoggedOnUserId();

        string strInvoiceID = "";


        foreach (GridViewRow row in gvPaymentInvoice.Rows)
        {
            if (row.RowType == DataControlRowType.DataRow)
            {
                CheckBox chkBox = (CheckBox)(row.Cells[0].FindControl("chkIsActive"));
                if (chkBox.Checked)
                {
                    int InvoiceID = Convert.ToInt32(gvPaymentInvoice.DataKeys[row.RowIndex].Value.ToString());
                    strInvoiceID = strInvoiceID + "," + Convert.ToString(InvoiceID);
                }
            }
        }


        objPIBO.InvoiceID = strInvoiceID.Substring(1, strInvoiceID.Length - 1);


        strReturnValue = (string)objPIBal.SetPaymentInvoice(objPIBO);
        if (strReturnValue == "SUCCESS")
        {

            ClearData();
            ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Payment paid succesfully.');", true);
            //lblMessage.Text = "Payment paid succesfully.";
            //lblMessage.ForeColor = System.Drawing.Color.Green;
            //GetData();

        }
        else if (strReturnValue == "EXIST")
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Payment Invoice already exists.');", true);
            //lblMessage.Text = "Payment Invoice already exists."; //EnrollResources.GetMessage(Message.InsertMessage); // 
            //lblMessage.ForeColor = System.Drawing.Color.Red;  
        }
        else
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Error while saving record.');", true);
            //lblMessage.Text = "Error while saving record."; //EnrollResources.GetMessage(Message.FailedMessage); // 
            //lblMessage.ForeColor = System.Drawing.Color.Red;
        }

    }


    protected void ddlUserName_SelectedIndexChanged(object sender, EventArgs e)
    {

        GetData();
    }
    protected void gvPaymentInvoice_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void gvPaymentInvoice_OnPageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        //throw new NotImplementedException();
        gvPaymentInvoice.PageIndex = e.NewPageIndex;
        gvPaymentInvoice.DataSource = GetDataSet("PENDING");
        gvPaymentInvoice.DataBind();
    }
    protected void gvPayedInvoice_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void gvPayedInvoice_OnPageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        //throw new NotImplementedException();
        gvPayedInvoice.PageIndex = e.NewPageIndex;
        gvPayedInvoice.DataSource = GetDataSet("PAYED");
        gvPayedInvoice.DataBind();
    }
}